package com.one.rope.mvp.web.service.sms;

import com.one.rope.mvp.basecore.third.aliyun.AliyunSmsSendDTO;
import com.one.rope.mvp.basecore.third.aliyun.AliyunSmsSendVO;
import com.one.rope.mvp.basecore.third.aliyun.AliyunSmsUtil;
import com.one.rope.mvp.basecore.bean.BusinessMessage;
import com.one.rope.mvp.basecore.bean.ResponseObject;
import com.one.rope.mvp.web.config.CommonConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 阿里云短信服务
 * <p>
 *
 * @author ： Weijian.Liu
 * @date ： 2020/3/15 0:31
 */
@Service
@Slf4j
public class AliyunSmsService {

  @Autowired
  CommonConfig commonConfig;

  /**
   * 发送验证码短信
   * @param mobile
   * @param code
   * @return
   */
  public ResponseObject sendCodeMessage(String mobile, String code){
    AliyunSmsSendDTO smsSendDTO = new AliyunSmsSendDTO();
    smsSendDTO.setAccessKeyId(commonConfig.getAliyunAccessKeyID());
    smsSendDTO.setAccessSecret(commonConfig.getAliyunAccessKeySecret());
    smsSendDTO.setTemplateCode(commonConfig.getVerifyCodeTemplate());
    smsSendDTO.setMobile(mobile);
    smsSendDTO.setContent(code);
    try {
      AliyunSmsSendVO smsSendVO = AliyunSmsUtil.sendTemplateMessage(smsSendDTO);
      if(!"OK".equals(smsSendVO.getCode())){
        return ResponseObject.userFailure(smsSendVO.getMessage());
      }
    }catch (Exception e){
      log.error("短信发送失败", e);
      return ResponseObject.userFailure(BusinessMessage.SMS_SEND_FAILURE);
    }
    return ResponseObject.successObject();
  }
}
